package com.qys.livesMall.kafka.dao.shop;

import com.qys.livesMall.kafka.entity.shop.ShopBrowseRecord;
import com.qys.livesMall.kafka.entity.shop.ShopGoodsInterestInfo;
import com.qys.livesMall.kafka.entity.shop.ShopInterestInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @menu ShopQueryHandle
 * @date 2023/06/02 19:27
 */
@Service
public class ShopQueryHandle {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 分页查询
     */

    public List<ShopBrowseRecord> shopBrowseRecordPageByUserId(Long userId,long page,long pageSize) {

        //1、创建Criteria对象，并设置查询条件
        Criteria criteria = Criteria.where("userId").is(userId);
        //2、根据Criteria创建Query
        Query queryLimit = new Query(criteria)
                .skip((page -1) * pageSize) //从第几条开始查询
                .limit(Integer.parseInt(String.valueOf(pageSize))) //每页查询条数
                .with(Sort.by(Sort.Order.desc("browseDate")));
        //3、查询
        List<ShopBrowseRecord> list = mongoTemplate.find(queryLimit, ShopBrowseRecord.class);
        return list;
    }

    /**
     * 分页查询
     */

    public List<ShopBrowseRecord> shopBrowseRecordPageByShopId(Long shopId,long page,long pageSize) {

        //1、创建Criteria对象，并设置查询条件
        Criteria criteria = Criteria.where("shopId").is(shopId);
        //2、根据Criteria创建Query
        Query queryLimit = new Query(criteria)
                .skip((page -1) * pageSize) //从第几条开始查询
                .limit(Integer.parseInt(String.valueOf(pageSize))) //每页查询条数
                .with(Sort.by(Sort.Order.desc("browseDate")));
        //3、查询
        List<ShopBrowseRecord> list = mongoTemplate.find(queryLimit, ShopBrowseRecord.class);
        return list;
    }


    /**
     * 分页查询
     */

    public Integer shopBrowseRecordCountByShopId(Long shopId) {

        //1、创建Criteria对象，并设置查询条件
        Criteria criteria = Criteria.where("shopId").is(shopId);
        //2、根据Criteria创建Query
        Query query = new Query(criteria);
        //3、查询
        List<Long> list = mongoTemplate.findDistinct(query,"userId", ShopBrowseRecord.class,Long.class);
        return list!=null?list.size():0;
    }

    /**
     * 分页查询
     */

    public List<ShopInterestInfo> shopInterestInfoPageByUserId(Long userId, long page, long pageSize) {

        //1、创建Criteria对象，并设置查询条件
        Criteria criteria = Criteria.where("userId").is(userId);
        //2、根据Criteria创建Query
        Query queryLimit = new Query(criteria)
                .skip((page -1) * pageSize) //从第几条开始查询
                .limit(Integer.parseInt(String.valueOf(pageSize))) //每页查询条数
                .with(Sort.by(Sort.Order.desc("browseDate")));
        //3、查询
        List<ShopInterestInfo> list = mongoTemplate.find(queryLimit, ShopInterestInfo.class);
        return list;
    }

    /**
     * 分页查询
     */

    public List<ShopGoodsInterestInfo> shopGoodsInterestInfoPageByUserId(Long userId, long page, long pageSize) {

        //1、创建Criteria对象，并设置查询条件
        Criteria criteria = Criteria.where("userId").is(userId);
        //2、根据Criteria创建Query
        Query queryLimit = new Query(criteria)
                .skip((page -1) * pageSize) //从第几条开始查询
                .limit(Integer.parseInt(String.valueOf(pageSize))) //每页查询条数
                .with(Sort.by(Sort.Order.desc("browseDate")));
        //3、查询
        List<ShopGoodsInterestInfo> list = mongoTemplate.find(queryLimit, ShopGoodsInterestInfo.class);
        return list;
    }

}

